//
//  ZZComposeTextView.swift
//  web
//
//  Created by 张卓 on 2017/2/8.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit

class ZZComposeTextView: UITextView {
    
    
    
    //供外界设置站位文字
    var placeholder: String? {
        didSet{
            //设置站位文字
            placeholderLabel.text = placeholder
        }
    }
    
    //重写font 属性.在didSet方法中监听它设置了多大的字体
    override var font: UIFont?{
        didSet{
            //设置站位文字的font
            placeholderLabel.font = font
        }
        
    }
    
    

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        addSubview(placeholderLabel)
        //设置约束
        placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 8))
        addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 1, constant: -10))
        //通过监听系统通知来监听textView的文字变化
        NotificationCenter.default.addObserver(self, selector: #selector(textChange), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
        
    }
    //监听文字变化
    func textChange(){
        placeholderLabel.isHidden = self.hasText
    }
    
    
    //MARK: - 懒加载控件
    //站位label
    private lazy var placeholderLabel: UILabel = {
        let lab = UILabel()
        lab.text = "what the fuck day"
        lab.textColor = UIColor.darkGray
        lab.numberOfLines = 0
        return lab
    }()

}
